package com.rem.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rem.chat.pojo.dataobject.ChatSessionUserDO;
import com.rem.chat.service.dto.UserSessionChatDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/6/16 15:08
 * @description:
 */
@Mapper
public interface ChatSessionUserMapper extends BaseMapper<ChatSessionUserDO> {
    @Select("SELECT " +
            "  csu.*, " +
            "  cs.last_message, " +
            "  cs.last_receive_time, " +
            "  CASE " +
            "    WHEN SUBSTR(csu.contact_id, 1, 1) = 'G' THEN " +
            "      (SELECT COUNT(1) FROM user_contact u WHERE u.contact_id = csu.contact_id) " +
            "    ELSE " +
            "      0 " +
            "  END memberCount " +
            "FROM " +
            "  chat_session_user csu " +
            "  INNER JOIN chat_session cs ON csu.session_id = cs.session_id " +
            "WHERE " +
            "  csu.user_id = #{userId} " +
            "ORDER BY cs.last_receive_time DESC ")
    List<UserSessionChatDTO> selectUserSessionChat(@Param("userId") String userId);
}
